package leecode

func robII(nums []int) int {
	if len(nums) < 2 {
		return nums[0]
	}
	//偷首 不偷尾
	result1 := conditions(nums[:len(nums)-1])
	//偷尾 不偷首
	result2 := conditions(nums[1:])
	return max(result1, result2)
}

func conditions(nums []int) int {
	if len(nums) < 2 {
		return nums[0]
	}
	//dp定义
	dp := make([]int, len(nums)) //到第i间房屋能够偷盗的最大值
	//初始化
	dp[0] = nums[0]
	dp[1] = max(nums[0], nums[1])
	result := max(dp[1], dp[0])
	//遍历
	for i := 2; i < len(nums); i++ {
		dp[i] = max(dp[i-2]+nums[i], dp[i-1])
		if dp[i] > result {
			result = dp[i]
		}
	}
	return result
}
